<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">欢迎来到学生信息管理系统</h3>
      </div>

      <!-- 账号 -->
      <el-form-item prop="userName">
        <span class="svg-container">
          <i style="font-size:20px;" class="el-icon-user-solid"></i>
        </span>
        <el-input
          ref="userName"
          v-model="loginForm.userName"
          placeholder="账号"
          name="userName"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <!-- 密码框 -->
      <el-form-item prop="userPassword">
        <span class="svg-container">
          <i style="font-size:20px;" class="el-icon-lock"></i>
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.userPassword"
          :type="passwordType"
          placeholder="密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <img
            style="width:25px;vertical-align: middle;"
            v-if="passwordType"
            src="./images/闭眼睛.png"
            alt
          />
          <img style="width:25px;vertical-align: middle;" v-else src="./images/眼睛_显示.png" alt />
        </span>
      </el-form-item>

      <el-button
        :loading="loading"
        type="primary"
        style="width:100%;margin-bottom:30px;"
        @click.native.prevent="handleLogin"
      >登录</el-button>

      <div class="tips">
        <span style="margin-right:20px;">账号: admin</span>
        <span>密码: 112233</span>
        <a style="color:red;margin-left:200px;cursor: pointer;" @click="$router.push('/register')">没有账号，立即注册</a>
      </div>
    </el-form>
  </div>
</template>

<script>

export default {
  name: "Login",
  data() {
    // 自定义用户名判断
    const validateUserName = (rule, value, callback) => {
      let trimValue = value.trim()
      if (!trimValue.trim()) {
        callback(new Error("账号或密码错误"));
      } else {
        callback();
      }
    };
    // 自定义密码判断
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("账号或密码错误"));
      } else {
        callback();
      }
    };
    return {
      // 表单信息
      loginForm: {
        userName: "",
        userPassword: ""
      },
      // 表单约束
      loginRules: {
        userName: [
          { required: true, trigger: "blur", validator: validateUserName }
        ],
        userPassword: [
          { required: true, trigger: "blur", validator: validatePassword }
        ]
      },
      // 控制提交按钮时否还在加载状态
      loading: false,
      // 定义密码的类型（是否显示）
      passwordType: "password",
      redirect: undefined
    };
  },
  watch: {
    // 监听路由
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      // 一上来先监听一次
      immediate: true
    }
  },
  methods: {
    a(){
      this.$router.push('/register');
    },
    // 查看密码
    showPwd() {
      // 判断密码显示与隐藏来回切换
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      // 点击完后自动获取焦点
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    // 登录事件
    handleLogin() {
      // 验证表单是否通过
      this.$refs.loginForm.validate(async valid => {
        // 当表单信息正确时
        if (valid) {
          // 按钮点击进入加载中
          this.loading = true;
          try {
            // 获取用户登录信息
            let result = await this.$store.dispatch( "studentAdminAccount/getUserLogin",this.loginForm);
            sessionStorage.setItem('userName',this.loginForm.userName)
            if (result.code === 200) {
              this.$message({
                message:'登录成功',
                type:'success'
              })
              // 登录成功路由跳转
              this.$router.push({ path: "/" || undefined });
              // 关闭按钮加载
              this.loading = false;
            }
          } catch (error) {
            this.loading = false;
            this.$message.error(`${error.message}`)
          }
        } else {
          console.log("error submit!!");
          this.loading = false;
          return false;
        }
      });
    }
  }
};
</script>

<style lang="less">
@bg: #283443;
@light_gray: #fff;
@cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: @cursor)) {
  .login-container .el-input input {
    color: @cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 0px;
      color: @light_gray;
      height: 47px;
      caret-color: @cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px @bg inset !important;
        -webkit-text-fill-color: @cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="less" scoped>
@bg: #2d3a4b;
@dark_gray: #889aa4;
@light_gray: #eee;

.login-container {
  // min-height: 100%;
  height: 100vh;
  width: 100%;
  background-color: @bg;
  background: url('./images/x.jpg') 0 -20px no-repeat ;
  background-size:cover;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: @dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: @light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: @dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
